﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TheQueens
{
    public partial class Form1 : Form
    {
        private fHelp childform;
        public Form1()
        {
            InitializeComponent();
            this.chessBoard1.Refresh(8);
            bt_Solve.Select();
            cb_QueensQuantity.Text = "8";
            cb_ResultOrder.Text = "1";
        } 

        #region Buttons Click Event

        private void bt_Solve_Click(object sender, EventArgs e)
        {
            //if correct
            //
            int iQueens = Int32.Parse(cb_QueensQuantity.Text);
            if (iQueens == chessBoard1.Queens)
            {
                chessBoard1.Solve();
            }
            else
            {
                chessBoard1.Queens = iQueens;
            }

            GetResults();
        }

        private void bt_Refresh_Click(object sender, EventArgs e)
        {
            chessBoard1.Refresh(Int32.Parse(cb_QueensQuantity.Text));
            cb_ResultOrder.Items.Clear();
            cb_ResultOrder.Text = "";
            lb_ResultNumber.Text = "Có < > cách giải";
            lb_QueenLocation.Text = "    ";
        }        

        private void cb_ResultOrder_SelectedIndexChanged(object sender, EventArgs e)
        {
            chessBoard1.ResultOrder = cb_ResultOrder.SelectedIndex;
        }

        private void bt_Next_Click(object sender, EventArgs e)
        {
            //if the problem was not solved, then solve it and show the first result 
            //
            if (chessBoard1.ResultsNumber == 0)
            {
                if (chessBoard1.Queens == Int32.Parse(cb_QueensQuantity.Text))
                {
                    chessBoard1.Solve();
                }
                else
                {
                    chessBoard1.Queens = Int32.Parse(cb_QueensQuantity.Text);
                }

                GetResults();
            }
            //else if the order of current result is less than the number of results
            //show the next result
            else if (cb_ResultOrder.SelectedIndex < cb_ResultOrder.Items.Count - 1)
            {
                chessBoard1.ResultOrder += 1;
                cb_ResultOrder.SelectedIndex += 1;
            }
            //else if the current result is the final result, show the first result
            else
            {
                chessBoard1.ResultOrder = 0;
                cb_ResultOrder.SelectedIndex = 0;
            }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            chessBoard1.Refresh(Int32.Parse(cb_QueensQuantity.Text));
            cb_ResultOrder.Items.Clear();
            switch (MessageBox.Show("Bạn có thực sự muốn thoát?", "Thông báo", MessageBoxButtons.OKCancel))
            {
                case DialogResult.OK:
                    Application.Exit();
                    break;
            }
            //Application.Exit();
        }

        private void bt_Help_Click(object sender, EventArgs e)
        {
            childform = new fHelp();
            childform.ShowDialog();
        }

        private void chessBoard1_MouseUp(object sender, MouseEventArgs e)
        {
            lb_QueenLocation.Text = "Vị trí đặt sẵn <" + chessBoard1.QueenX.ToString() + ", " + chessBoard1.QueenY.ToString() + ">";
        }

        private void cb_QueensQuantity_TextChanged(object sender, EventArgs e)
        {
            lb_QueenLocation.Text = "  ";
        }
        #endregion

        #region Other methods

        private void GetResults()
        {
            if (chessBoard1.ResultsNumber > 0)
            {

                cb_ResultOrder.Items.Clear();

                int itemp = chessBoard1.ResultsNumber;

                for (int ii = 0; ii < itemp; ii++)
                {
                    cb_ResultOrder.Items.Add(ii + 1);
                }

                cb_ResultOrder.SelectedIndex = 1;

                lb_ResultNumber.Text = "Có < " + itemp.ToString() + " > cách giải";
            }
            else
            {
                MessageBox.Show("Can not sovled the problem", "ERROR", MessageBoxButtons.OK);
                return;
            }
        }

        #endregion
    }
}
